window.BILL = {};
BILL.createBillObject = function(parentDom,BCP){
	var bill = {
		model : null,
		oldModel : null,
		isInited : false,
		parentDom : parentDom,
		BCP : BCP,
		runtimeId : null,
		h5SubTables : [],
		currentH5SubTable : null,
		dataDicts : [],
		add : function(){
			var obj = {
				runtimeId:"",
				methodParameters: []
			};
			
			BCP.invokeBizMethod('add',obj , function(data){
				var responseObject = JSON.parse(data);
				bill.runtimeId = responseObject.runtimeId;
				
				if(responseObject.status == 'success'){
					var result = responseObject.result;
					var resultObject = JSON.parse(result);
					
					bill.oldModel = JSON.parse(result);
					
					if(!bill.isInited){
						BCP.initModel(resultObject);
						bill.isInited = true;
					}else {
						BCP.setModel(resultObject);
					}
					
					//BCP.fireCustomEvent("listBasedataResetControlValue");
					//BCP.fireCustomEvent("listBasedataInitControlValue");
					for(var i=0;i<bill.h5SubTables.length;i++){
						var h5SubTable = bill.h5SubTables[i];
						h5SubTable.resetSubDatas();
						h5SubTable.initSubDatas(resultObject);
					}
				}else if(responseObject.status == 'programException'){
					var message = responseObject.result;
					alert(message);
				}else if(responseObject.status == 'businessException'){
					var message = responseObject.result;
					alert(message);
				}else if(responseObject.status == 'validateErrorException'){
					var message = responseObject.result;
					alert(message);
				}else if(responseObject.status == 'validateAlertException'){
					var message = responseObject.result;
					alert(message);
				}
			});
		},
		load : function(bizDataId){
			var modelData = BCP.getModel();
			
			var obj = {
				runtimeId:"",
				methodParameters: [bizDataId]
			};
			
			BCP.invokeBizMethod('load',obj , function(data){
				var responseObject = JSON.parse(data);
				bill.runtimeId = responseObject.runtimeId;
				
				if(responseObject.status == 'success'){
					var result = responseObject.result;
					var resultObject = JSON.parse(result);
					
					bill.oldModel = JSON.parse(result);
					
					if(!bill.isInited){
						BCP.initModel(resultObject);
						bill.isInited = true;
					}else {
						BCP.setModel(resultObject);
					}
					
					//BCP.fireCustomEvent("listBasedataResetControlValue");
					//BCP.fireCustomEvent("listBasedataInitControlValue");
					for(var i=0;i<bill.h5SubTables.length;i++){
						var h5SubTable = bill.h5SubTables[i];
						h5SubTable.resetSubDatas();
						h5SubTable.initSubDatas(resultObject);
					}
				}else if(responseObject.status == 'programException'){
					var message = responseObject.result;
					alert(message);
				}else if(responseObject.status == 'businessException'){
					var message = responseObject.result;
					alert(message);
				}else if(responseObject.status == 'validateErrorException'){
					var message = responseObject.result;
					alert(message);
				}else if(responseObject.status == 'validateAlertException'){
					var message = responseObject.result;
					alert(message);
				}
			});
		},
		updateMainTableAttribute : function(mainAttriName,mainAttriValue){
			if(mainAttriValue == this.oldModel[mainAttriName]){
				return;
			}
				
			var obj = {
				runtimeId:bill.runtimeId,
				methodParameters: [mainAttriName,mainAttriValue]
			};
			
			BCP.invokeBizMethod('updateMainTableAttribute',obj , function(data){
				var responseObject = JSON.parse(data);
				bill.runtimeId = responseObject.runtimeId;
				
				if(responseObject.status == 'success'){
					var serverChangeInfo = responseObject.serverChangeInfo;
					serverChangeInfo = JSON.parse(serverChangeInfo);
					
					bill.processServerChangeInfo(serverChangeInfo);
				}else if(responseObject.status == 'programException'){
					var message = responseObject.result;
					alert(message);
				}else if(responseObject.status == 'businessException'){
					var message = responseObject.result;
					alert(message);
				}else if(responseObject.status == 'validateErrorException'){
					var message = responseObject.result;
					alert(message);
				}else if(responseObject.status == 'validateAlertException'){
					var message = responseObject.result;
					alert(message);
				}
			});
		},
		save : function(){
			var obj = {
				runtimeId:bill.runtimeId,
				methodParameters: []
			};
			
			BCP.invokeBizMethod('save',obj , function(data){
				var responseObject = JSON.parse(data);
				bill.runtimeId = responseObject.runtimeId;
					
				if(responseObject.status == 'success'){
					var result = responseObject.result;
					var resultObject = JSON.parse(result);
					
					alert('保存成功!');
				}else if(responseObject.status == 'programException'){
					var message = responseObject.result;
					alert(message);
				}else if(responseObject.status == 'businessException'){
					var message = responseObject.result;
					alert(message);
				}else if(responseObject.status == 'validateErrorException'){
					var message = responseObject.result;
					var msgArray = JSON.parse(message);
					if(msgArray.length > 0){
						alert(msgArray[0].msg);
					}
				}else if(responseObject.status == 'validateAlertException'){
					var message = responseObject.result;
					var msgArray = JSON.parse(message);
					if(msgArray.length > 0){
						alert(msgArray[0].msg);
					}
				}
			});
		},
		submit : function(){
			var modelData = BCP.getModel();
						
			var obj = {
				runtimeId:bill.runtimeId,
				methodParameters: [BCP.businessType,modelData.id]
			};
			
			BCP.invokeBizMethod('submit',obj , function(data){
				var responseObject = JSON.parse(data);
				bill.runtimeId = responseObject.runtimeId;
					
				if(responseObject.status == 'success'){
					var result = responseObject.result;
					var resultObject = JSON.parse(result);
					if(resultObject.statusCode != 0){
						alert(resultObject.message);
					}else{
						alert('提交成功!');
					}
					
				}else if(responseObject.status == 'programException'){
					var message = responseObject.result;
					alert(message);
				}else if(responseObject.status == 'businessException'){
					var message = responseObject.result;
					alert(message);
				}else if(responseObject.status == 'validateErrorException'){
					var message = responseObject.result;
					alert(message);
				}else if(responseObject.status == 'validateAlertException'){
					var message = responseObject.result;
					alert(message);
				}
			});
			
		},
		processServerChangeInfo : function(serverChangeInfo){
			for(var index=0;index<serverChangeInfo.length;index++){
				var serverChangeInfoItem = serverChangeInfo[index];
				var modelData = BCP.getModel();
				
				if(serverChangeInfoItem.changeType == 'MainAttributeChangeItem'){
					modelData[serverChangeInfoItem.attriName] = serverChangeInfoItem.attriValue;
					bill.oldModel[serverChangeInfoItem.attriName] = serverChangeInfoItem.attriValue;
					
					BCP.setModel(modelData);
				}
			}
			
			//BCP.fireCustomEvent("listBasedataProcessServerChangeInfo",serverChangeInfo);
			for(var i=0;i<bill.h5SubTables.length;i++){
				var h5SubTable = bill.h5SubTables[i];
				h5SubTable.subTableProcessServerChangeInfo(serverChangeInfo);
			}
		}
	};
	return bill;
};